

$.ajaxSetup ({
     
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
           
    cache: false
});


function del(word, listName){
    $.get("delete.jsp",  {
        word: word,  
        listName:listName
    }, function(data2) {
        
        $.get("retrieve.jsp", {
            listName: listName
        }, function(data){
            $("#wordlist").html(data);
        });
        retrieveMyList();
    });
}
function wordClick(o, word, level) {
    for(i=0; i<=4; i++)
    {
        $("#b"+i+word).css("color",  "#333");
    }
    var oldSrc = (document.getElementById(word)==null?null:document.getElementById(word).src) ;
    var newSrc ="";
    if (level ==0) {
        newSrc =  "http://www.onelook.com/?w=" + word;
    }
    else if (level<4)
    {
        newSrc =  "http://www.wordsmyth.net/?ent=" + word + "&level=" + level;
    }
    else if(level==4)
    {
        newSrc = "http://www.elookinto.com/search?q="+word+"&tbs=dfn:1";
    }
    if (oldSrc != newSrc)
    {
        if (document.getElementById(word) != null)
            document.getElementById(word).src = newSrc;
        if ($("#"+word).css("display") != "none")
        {
            $(o).css("color",  "red");
            return;
        }
    }
    if ($("#"+word).css("display") == "none") {
        $("#"+word).css("display", "block");
        $(o).css("color",  "red");
    }
    else
        $("#"+word).css("display", "none"); 
//alert("adfd");
}

function retrieveGradeList(grade)
{
    
    $.get("retrieveGradeList.jsp", {
        grade:grade
    }, function(data){
        $("#gradeList").html(data); 
    });
}

function retrieveSATList(grade)
{
    
    $.get("retrieveSATList.jsp", {
        grade:grade
    }, function(data){
        $("#gradeList").html(data); 
    });
}

function retrieveGREList()
{
    
    $.get("retrieveGREList.jsp", function(data){
        $("#gradeList").html(data); 
    });
}

function retrieveTOEFLList()
{
    
    $.get("retrieveTOEFLList.jsp", function(data){
        $("#gradeList").html(data); 
    });
}

function upload(o)
{
                 
    listname = $.trim($('#listname').val());
    
    
    if (listname.length==0) 
    {
        if ($.cookie('currentWordList')!=null)
            listname = $.cookie('currentWordList');
        else
            listname= (new Date()).toString().substring(0,24).replace(/\W/g, '_');
        $('#listname').val(listname);
    }  
    if ($('#inputarea').css("display") == "none") {
        $('#inputarea').css('display', 'block');
        //$('#message').css('display', 'block');           
        $(o).text("Save");
            
    } else {
     
        $('#inputarea').css('display', 'none');
                 
        $(o).text("Upload words");
        text = $.trim($('#upload').val());
                    
        if (text.length!=0)
        {
            var loc = window.location +"";
            //alert(loc); 
            dir ="";
            if (loc.indexOf("/m")>0) dir="../"
            
            $.post(dir+"save.jsp", 
            {
                wordList:text, 
                listName: listname,
                note: $("#note").val()
            },
            function(data2){
                $.cookie("currentListName", listname);
                retrieveMyList();
                $("#message").text("Your list '"+listname +"' has been saved!");
                var words = text.split(/\W+/);
                var currentWord = words[0];
                $.get(dir+"retrieve.jsp", {
                    listName: listname,
                    currentWord: currentWord
                }, function(data) {
                    // alert(data);
                    $("#wordlist").html(data);
                    $('#listname').val("");
                    $('#upload').val("");
                }); 
            });
                        
                        
        }
    }
}
     
function uploadRecording(o)
{
                 
    listname = $.trim($('#listname').val());
    
    
    if (listname.length==0) 
    {
        alert("You must have a list name");
            
    } else {
     
        $('#inputarea').css('display', 'none');
                 
        $(o).text("Upload words");
        text = $.trim($('#upload').val());
                    
        if (text.length!=0)
        {
            $.post("save.jsp", 
            {
                wordList:text, 
                listName: listname,
                note: $("#note").val()
            },
            function(data2){
                $.cookie("currentListName", listname);
                retrieveMyList();
                $("#message").text("Your list '"+listname +"' has been saved!");
                $.get("retrieve.jsp", {
                    listName: listname
                }, function(data) {
                    // alert(data);
                    $("#wordlist").html(data);
                    $('#listname').val("");
                    $('#upload').val("");
                }); 
            });
                        
                        
        }
    }
}
             
function wordList(listName, pub) {
    if (isNaN(listName))
        $.cookie("currentWordList", listName, {
            expires: 365
        });

    $.cookie("pub", pub, {
        expires: 365
    });
    //alert($.cookie("currentWordList"));
    $.get("retrieve.jsp", {
        listName: listName,
        pub: pub
    },
    function (data){
        $("#wordlist").html(data);
        $('a.button').qtip();
    });
        
}
function getPlayer(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}
fixedsrc="http://www.strangecube.com/audioplay/online/audioplay.swf?file=http://onelook.com/pronounce/macmillan/US/word-American-English-pronunciation.mp3&auto=no&sendstop=yes&repeat=1&buttondir=http://www.strangecube.com/audioplay/online/alpha_buttons/negative_small&bgcolor=0xffffff&mode=playpause&einterface=yes";

function play1(o)
{
               
    getPlayer('test').src = fixedsrc.replace(/word/g, o);
    for(i=0; i<100000;i++);

    //alert( getPlayer('test').src);
    getPlayer('test').playMusic();
}


function hideAll()
{
    $("iframe").css("display", "none");
    $("a.button").css("color", "#333");
}

function retrieveMyList()
{
     
    $.get("retrieveMyList.jsp", function(data) {
        // alert(data);
        $("#myWordList").html(data);
    }); 
}

//Deprecated.
function retrieveExisting(ob)
{
    newwords = $.trim($("#upload").val());
    listName = $.trim(ob.value);
    if (listName.length==0) return;
    $.get("retrieveExisting.jsp",{
        listName: listName
    }, function(data) {
        if (data == null || data.length==0 ) return;
        words = data.split(" ");
        for(i=0; i<words.length;i++) {
            if (newwords.indexOf($.trim(words[i]))<0) {
                //alert(newwords);
                //alert("--"+words[i]+"--");
                newwords = data + "\n\n"+ newwords;
                $("#upload").val(newwords);
                break;
            }
                
        }
    // alert(data);
    //$("#myWordList").html(data);
    });
}


function limitText(limitField, limitCount, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}

function check()
{
    AtD.checkTextAreaCrossAJAX('upload', 'checkLink', 'Edit Text');
}
        
function insertWordToUpload(word, listName)
{
    if ($("#upload").val().indexOf(word) >=0) return;
     
    if (listName != $('#listname').val())
    {
       
        //alert(listName);
        $("#upload").val($("#upload").val() +" " + word);
    }
    else
    {
        $("#message").text("Give your new list a new name because it has the same name as the list below!");
    }
}


$( "#dialog" ).dialog({
    autoOpen: false
});
$( "#opener" ).click(function() {
    $( "#dialog" ).dialog( "open" );
});

function choose_dict()
{

    $ ("#dialog" ).dialog({
        resizable: false,
        height:280,
        width:400,
        modal: true,
        close: function() {
            transformButton();
        }
    /*buttons: {
            "Delete The Test Case": function() {
                 
                $.ajax(  {
                    url:currentTestCaseUrl,
                    type: "DELETE"
                } );
                jQuery( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }*/
    });

}

function transformButton()
{
    //$.cookie
    $('input:checkbox').each(function(){
        //alert($.cookie($(this).attr("id")));
        if ($.cookie($(this).attr("id")) == "true") {
            $(this).prop("checked", $.cookie($(this).attr("id")));
        } else {
            $(this).prop("checked", false);
        }
        
    });
    $('.button').each(function(){
        id = $(this).attr("id");
        if ((id.indexOf("b4")>=0 && $("#choose_4").prop("checked"))|| $("input:checked").length ==0 ){
            //if ($("#choose_4").attr(":checked"))
            $(this).children(":first").text("Google Dict");
            $(this).show();
        } else {
            $(this).hide(); 
        }
        if (id.indexOf("b3")>=0){
            if ($("#choose_3").prop("checked")) {
                $(this).children(":first").text("Advanced Wordsymth");
                $(this).show();
            }
            else 
                $(this).hide();
        } 
       
        if (id.indexOf("b2")>=0){
            if ($("#choose_2").prop("checked")) {
                $(this).children(":first").text("Children's Wordsymth");
                $(this).show();
            }
            else 
                $(this).hide();
        } 
        if (id.indexOf("b1")>=0){
            if ($("#choose_1").prop("checked")) {
                $(this).children(":first").text("Beginner's Wordsymth");
                $(this).show();
            }
            else 
                $(this).hide();
        } 
        if (id.indexOf("b0")>=0){
            if ($("#choose_0").prop("checked")){
                $(this).children(":first").text("OneLook");
                $(this).show();
            }
            else 
                $(this).hide();
        } 
       
       
    }
    );
}

$(function() {
    $( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 550,
        modal: true,
        buttons: {
            "Rename List": function() {
                var oldName = $("#oldListName").text();
                var newName = $("#newListName").val();
                if (oldName == newName) {
                    alert ("New list name must different from old list name ");
                    return;
                }
                $.get("renameList.jsp", {
                    oldListName: oldName,
                    newListName: newName
                }, function(data)
                {
                    retrieveMyList();
                   $( "#dialog-form" ).dialog( "close" );
                });
            
               
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
        //allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
});